# Copyright 2011 Fractus Labs (www.fractus-labs.com)
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""Abstraction of the underlying os API."""

import sys

from fractus import flags
from fractus import log as logging
from fractus.os import fake 
from fractus.os import linux_os
from fractus.os import windows_os


LOG = logging.getLogger("fractus.os.os")
FLAGS = flags.FLAGS


def get_os(read_only=False):
    """
    Returns an object representing the os of the node.

    This could be :mod:`fractus.os.fake.FakeConnection` in test mode,
    a os to linux via :mod:`linux_os`, or a connection
    to windows via :mod:`windows_os`.

    Any object returned here must conform to the interface documented by
    :mod:`FakeConnection`.

    **Related flags**

    :connection_type:  A string literal that falls through a if/elif structure
                       to determine what os mechanism to use.
                       Values may be

                            * fake
                            * linux
                            * windows
    """
    t = FLAGS.connection_type
    if t == 'fake':
        conn = fake.get_connection(read_only)
    elif t == 'linux':
        conn = libvirt_conn.get_connection(read_only)
    elif t == 'windows':
        conn = xenapi_conn.get_connection(read_only)
    else:
        raise Exception('Unknown os type "%s"' % t)

    if conn is None:
        LOG.error(_('Failed to get os driver'))
        sys.exit(1)
    return conn
